package com.summli.basic.class07;

/**
 * 二叉树的先序、中序、后序遍历
 * 先序：任何子树的处理顺序都是，先头节点、再左子树、然后右子树
 *
 * 中序：任何子树的处理顺序都是，先左子树、再头节点、然后右子树
 *
 * 后序：任何子树的处理顺序都是，先左子树、再右子树、然后头节点
 */
public class Code01_RecursiveTraversalBT {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }
    // 先序
    public static void pre(Node head) {
        if(head == null){
            return;
        }
        System.out.println(head.value);
        pre(head.left);
        pre(head.right);
    }
    // 中序
    public static void mid(Node head){
        if(head == null){
            return;
        }
        mid(head.left);
        System.out.println(head.value);
        mid(head.right);
    }
    // 后序
    public static void pos(Node head){
        if(head == null){
            return;
        }
        pos(head.left);
        pos(head.right);
        System.out.println(head.value);
    }

    //for test
    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        pre(head);
        System.out.println("========");
        mid(head);
        System.out.println("========");
        pos(head);
        System.out.println("========");

    }
}
